#include "vertexbuffer.h"
#include "misc.h"


vertexbuffer::vertexbuffer():
	mVertexes(NULL)
{
}


vertexbuffer::~vertexbuffer()
{
	delete mVertexes;
	mVertexes = NULL;
}

void vertexbuffer::SetSize(int vertexCount)
{
	mVertexCount = vertexCount;
	mVertexes = new VertexA[vertexCount];
	mVBO = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexA)*mVertexCount, GL_STATIC_DRAW, nullptr);
}

void vertexbuffer::SetPosition(int index, float x, float y, float z, float w)
{
	mVertexes[index].Position[0] = x;
	mVertexes[index].Position[1] = y;
	mVertexes[index].Position[2] = z;
	mVertexes[index].Position[3] = w;
}

void vertexbuffer::SetColor(int index, float r, float g, float b, float a)
{
	mVertexes[index].Color[0] = r;
	mVertexes[index].Color[1] = g;
	mVertexes[index].Color[2] = b;
	mVertexes[index].Color[3] = a;
}

void vertexbuffer::SetTexcoord(int index, float x, float y)
{
	mVertexes[index].Texcoord[0] = x;
	mVertexes[index].Texcoord[1] = y;
}

void vertexbuffer::SetNormal(int index, float x, float y, float z)
{
	mVertexes[index].Normal[0] = x;
	mVertexes[index].Normal[1] = y;
	mVertexes[index].Normal[2] = z;
}

void vertexbuffer::Bind()
{
	glBindBuffer(GL_ARRAY_BUFFER, mVBO);
	glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexA)*mVertexCount, (void*)mVertexes);	
}

void vertexbuffer::Unbind()
{
	glBindBuffer(GL_ARRAY_BUFFER,0);
}

VertexA vertexbuffer::Get(int index)
{
	if (mVertexes!=NULL&&mVertexCount>index)
	{
		return mVertexes[index];
	}
	return VertexA();
}
